local addonName = "PSLibBar";
local moduleName = "Panels";
local Addon = LibStub("AceAddon-3.0"):GetAddon(addonName);
local Module = Addon:GetModule(moduleName);

local L = LibStub("AceLocale-3.0"):GetLocale(addonName);

local panelTemplateOptionTable = 
{
	name = L["Panels.Settings.Template.DefaultName"],
	type = "group",
	args = 
	{ 
		["Panels.Settings.Template.MainGroup"] =
		{
			order = 1.0,
			type = "group",
			--inline = true,
			name = L["Panels.Settings.Template.MainGroup"],
			args = 
			{
				["Panels.Settings.Template.Name"] = 
				{
					order = 1.0,
					name = L["Panels.Settings.Template.Name"],
					type = "input",
				},
				
				["Panels.Settings.Template.Visible"] = 
				{
					order = 1.1,
					name = L["Panels.Settings.Template.Visible"],
					type = "toggle",
				},
			},
		},
		
		["Panels.Settings.Template.LayoutGroup"] =
		{	
			order = 1.1,
			name = L["Panels.Settings.Template.LayoutGroup"],
			type = "group",
			--inline = true,
			args = 
			{
				["Panels.Settings.Template.Dock"] = 
				{
					order = 1.0,
					name = L["Panels.Settings.Template.Dock"],
					dialogControl = "PSLib.Anchor",
					values = setmetatable({}, 
					{ 
						__index = function(self,k) 
							if(k == "__settings") then
								return { selectMode = "SINGLE"};
							else return true; end
						end
					} ),
					type="select",
				},
				br1 = 
				{
					order = 1.1,
					type="description",
					name=""
				},
				["Panels.Settings.Template.SizeMode"] = 
				{
					order = 1.2,
					name = L["Panels.Settings.Template.SizeMode"],
					values = L["Panels.Settings.Template.SizeModeValues"],
					type = "select",
				},
				["Panels.Settings.Template.Height"] = 
				{
					order = 1.3,
					name = L["Panels.Settings.Template.Height"],
					type = "range",
					min = 10,
					max = 25,
					step = 1,
				},
			},
		},
		
		Plugins = 
		{
			type= "group",
			name = "Plugins",
			args = 
			{
				
			}
		}
	}
}

local panelTemplateDefaults = 
{
	["Panels.Settings.Template.Name"] = L["Panels.Settings.Template.DefaultName"],
	["Panels.Settings.Template.Visible"] = true,
	["Panels.Settings.Template.SizeMode"] = "auto",
	["Panels.Settings.Template.Dock"] = { Selected = {"TOP"}},
	["Panels.Settings.Template.Height"] = 16,
}

function Module:GetPanelTemplateDefaults()
	return PS:CloneObject(panelTemplateDefaults);
end

local function ApplyPanelSettings(self,settings)
	self.optionTable.name = settings["Panels.Settings.Template.Name"];
	
	if(settings["Panels.Settings.Template.Visible"]) then self:Show();
	else self:Hide(); end;
	
	self:ClearAllPoints();
	local dock = settings["Panels.Settings.Template.Dock"];
	if(dock.Selected and #dock.Selected == 1) then
		local point = dock.Selected[1];
		local dx,dy = 0,0;
		if(dock.Points) then
			dx = dock.Points[point].dx;
			dy = dock.Points[point].dy;
		end
		self:SetPoint(point,dx,dy);
	else
		self:Hide();
	end
	
	if(settings["Panels.Settings.Template.SizeMode"] == "auto") then
		self:SetWidth(40);
	elseif(settings["Panels.Settings.Template.SizeMode"] == "stretch") then
		self:SetWidth(UIParent:GetWidth());
	end
	if(settings["Panels.Settings.Template.Height"]) then
		self:SetHeight(settings["Panels.Settings.Template.Height"]);
	end
	
end

function Module:CreatePanel()	
	local panel = CreateFrame("Frame");
	panel:SetFrameStrata("FULLSCREEN_DIALOG");
	panel.bg = panel:CreateTexture(nil, "BACKGROUND");
	panel.bg:SetTexture(1,0,1);
	panel.bg:SetAllPoints(panel);
	panel:SetPoint("CENTER",0,0);
	panel:SetWidth(100);
	panel:SetHeight(30);
	panel:Show();
	
	panel.optionTable = PS:CloneObject(panelTemplateOptionTable);
	
	panel.ApplySettings = ApplyPanelSettings;
	
	panel.Dispose = function(self)
		self:Hide();
	end
	return panel;
end